package com.zan.springcloud.controller;

import com.zan.springcloud.entity.CommonResult;
import com.zan.springcloud.entity.Payment;
import com.zan.springcloud.lb.LoadBalancer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

/**
 * @Author Zan
 * @Create 2023/12/8 19:47
 * @Description :
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/consumer")
public class OrderController {

//    public static final String PAYMENT_URL = "http://localhost:8001/payment";
    /**
     * 这里得给RestTemplate赋予负载均衡得能力，在@Bean下添加@LoadBalanced —— 使用@LoadBalanced注解赋予RestTemplate负载均衡的能力
     * 要规定一种默认得负载均衡机制
     */
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE/payment"                                                                                                                                                                                                                             ;

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private LoadBalancer loadBalancer;

    @Resource
    private DiscoveryClient discoveryClient;

    /**
     * 客户端发起请求都是GET请求
     * @param payment
     * @return
     */
    @GetMapping("/payment/create")
    public CommonResult<Payment> createPayment(Payment payment) {
        return restTemplate.postForObject(PAYMENT_URL + "/create", payment, CommonResult.class);
    }

    @GetMapping("/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
        return restTemplate.getForObject(PAYMENT_URL + "/get/" + id, CommonResult.class);
    }

    @GetMapping("/payment/getForEntity/get/{id}")
    public CommonResult<Payment> getPayment2(@PathVariable("id") Long id) {
        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(PAYMENT_URL + "/get/" + id, CommonResult.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            return responseEntity.getBody();
        } else {
            return new CommonResult<>(500, "查询失败");
        }
    }

    @GetMapping("/payment/postForEntity/create")
    public CommonResult<Payment> createPayment2(Payment payment) {
        return restTemplate.postForEntity(PAYMENT_URL + "/create", payment, CommonResult.class).getBody();
    }

    @GetMapping("/payment/lb")
    public String getPaymentLB() {
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if (instances == null || instances.size() <= 0) {
            return null;
        }

        ServiceInstance serviceInstance = loadBalancer.instances(instances);
        URI uri = serviceInstance.getUri();

        return restTemplate.getForObject(uri + "/payment/lb", String.class);
    }

    // ====================> zipkin+sleuth
    @GetMapping(value = "/payment/zipkin")
    public String paymentZipkin() {
        String result = restTemplate.getForObject("http://localhost:8001"+"/payment/zipkin/", String.class);
        return result;
    }
}
